<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Home\Model;
use Think\Model;
use Think\Page;

/**
 * 文档基础模型
 */
class OrderModel extends Model{

    /* 自动验证规则 */
    protected $_validate = array(
        // array('name', '/^[a-zA-Z]\w{0,30}$/', '文档标识不合法', self::VALUE_VALIDATE, 'regex', self::MODEL_BOTH),
        // array('name', '', '标识已经存在', self::VALUE_VALIDATE, 'unique', self::MODEL_BOTH),
        // array('title', 'require', '标题不能为空', self::VALUE_VALIDATE, 'regex', self::MODEL_BOTH),
        // array('category_id', 'require', '分类不能为空', self::MUST_VALIDATE , 'regex', self::MODEL_INSERT),
        // array('category_id', 'require', '分类不能为空', self::EXISTS_VALIDATE , 'regex', self::MODEL_UPDATE),
        // array('category_id,type', 'check_category', '内容类型不正确', self::MUST_VALIDATE , 'function', self::MODEL_INSERT),
        // array('category_id', 'check_category', '该分类不允许发布内容', self::EXISTS_VALIDATE , 'function', self::MODEL_BOTH),
        // array('model_id,category_id,pid', 'check_category_model', '该分类没有绑定当前模型', self::MUST_VALIDATE , 'function', self::MODEL_INSERT),
    );

    /* 自动完成规则 */
    protected $_auto = array(
        // array('uid', 'session', self::MODEL_INSERT, 'function', 'user_auth.uid'),
        // array('title', 'htmlspecialchars', self::MODEL_BOTH, 'function'),
        // array('description', 'htmlspecialchars', self::MODEL_BOTH, 'function'),
        // array('root', 'getRoot', self::MODEL_BOTH, 'callback'),
        // array('attach', 0, self::MODEL_INSERT),
        // array('view', 0, self::MODEL_INSERT),
        // array('comment', 0, self::MODEL_INSERT),
        // array('extend', 0, self::MODEL_INSERT),
        // array('create_time', NOW_TIME, self::MODEL_INSERT),
        // //array('reply_time', NOW_TIME, self::MODEL_INSERT),
        // array('update_time', NOW_TIME, self::MODEL_BOTH),
        // array('status', 'getStatus', self::MODEL_BOTH, 'callback'),
    );

    public $page = '';

    /**
     * 获取文档列表
     * @param  integer  $category 分类ID
     * @param  string   $order    排序规则
     * @param  integer  $status   状态
     * @param  boolean  $count    是否返回总数
     * @param  string   $field    字段 true-所有字段
     * @return array              文档列表
     */
    
    public function orderByOrderIdGet($uid,$order_id){
        $orderData = $this->getOrderByOrderId($uid,$order_id);
        $contactsData = $this->getcontactsByOrderId($order_id);
        $orderData = $orderData['0'];
        $orderData['contacts'] = $contactsData;
        $articleshow_data = $this->articleShowDetialByOrderId($order_id);
        $orderData['articleshow'] = $articleshow_data;
        return $orderData;
    }
    public function addOrder($order_info,$uid){

            $order_id = $this
            ->data($order_info)
            ->add();
            return $order_id;
    }

    public function getOrderByOrderId($uid,$order_id){
      
        $orderWhere['order_id'] = $order_id;
        $orderWhere['uid'] = $uid;
        $orderData = $this
        ->where($orderWhere)
        ->select();
        return $orderData;
    }

    public function getOrders($filter){
        $order_data = $this
        ->where($filter)
        ->select();
        return $order_data;
    }



    public function getOrdersByType($uid,$type = 0){
        
        $order_where['uid'] = $uid;
        if($type){
            $order_where['order_type'] = $type;  
        }
        
        $order_id_data = $this
        ->where($order_where)
        ->field('id')
        ->select();
        
        foreach ($order_id_data as $key => $value) {
            $order_data[] = $this->orderByOrderIdGet($value['id']);
        }
        return $order_data; 
    }

    protected function getcontactsByOrderId($order_id){

        $contacts = D('contacts');
        $contactsData = $contacts->getcontactsByOrderId($order_id);
        return $contactsData;
    }

    protected function articleShowDetialByOrderId($order_id){
        $articleShowModel = D('articleshow');
        $articleshow_data = $articleShowModel-> articleShowDetialByOrderIdGet($order_id);
        return $articleshow_data;
    }

    public function cancelOrder($uid,$order_id){
      
        $where  = array(
            'uid'   => $uid, 
            'id'    => $order_id
            );
       
        $orderinfo =$this->where($where)->find();
        if($orderinfo['status']=='1'){
            //未支付的取消变成2；已取消
            $data['status']=2;
        }if($orderinfo['status']=='3'||$orderinfo['status']=='5'){
            //已支付的订单取消变成退款中
            $data['status']=4;//退款中
        }     
        if($data){
           $order_data = $this
                    ->where($where)
                    ->data($data)
                    ->save(); 
        }  
        
        return $order_data;
    }
}
